//********************************************************************************
//
// File :           ph01_calc_pi_cluster.cpp
//
// Description :    calculate pi
//
// Dependencies:
//
// Author :         UlMonkey1987( WU Kan )
//
// Stu NO. :        2010212404
//
// Date :           2011-3-23
//
//********************************************************************************


#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <string>
#include <fstream>

using namespace std;

// double calcF( const double x )
double calcF( const double x )
{
    return ( 4.0 / ( 1.0 + x * x ) );
}
//

int main( int argc, char* argv[] )
{
    if ( argc < 3 )
    {
        cout << "error : lack args." << endl;
        cout << "usage : " << "<program_name>" << " <n> <output_file_path>." << endl;
        return 1;
    }

    long long int n = atoi( argv[1] );      // n
    string strOutPath = string( argv[2] );      // output path
    double finalPi;                         //  final result
    double finalSum = 0;                    // final sum

    clock_t timeStart, timeEnd;
    double timeCost;                    // start time, end time, duration time

    timeStart = clock();

    for ( long long int i = 1; i <= n; i ++ )
    {
        finalSum += calcF( ( i - 0.5 ) * 1.0 / n );
    }

    finalPi = finalSum * 1.0 / n;

    timeEnd = clock();
    timeCost = ( timeEnd - timeStart ) * 1.0 / (CLOCKS_PER_SEC);

    ofstream fout( strOutPath.c_str(), ios::app );
    fout << "non_cluster\t" << n << "\t" << setprecision( 20 ) << finalPi << "\t" << setprecision( 6 ) << abs( finalPi - acos( -1.0 ) ) << "\t" << timeCost << "\n";

    //fout << "Calculation Result : PI = " << setprecision( 20 ) << finalPi << endl << endl;

    //fout << "Time Consumed : " << setprecision( 6 ) << timeCost << " Ms" << endl << endl;

    return 0;
}


